package com.zz.common.tools.box.tools;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.util.Log;

import com.zz.common.tools.box.tools.BpUrlLoader.IBpOnUrlLoaderCompleteListener;
import com.zz.common.tools.box.tools.BpUrlLoader.IBpOnUrlLoaderErrorListener;

public class BpDataLoader extends BpFuture {
	public interface IBpOnDataLoaderCompleteListener {
		public void onComplete(int errCode, Object obj, Map<String, List<String>> headers, BpDataLoader loader);
	}
	
	public static final int DATA_LOADER_PARSE_ERROR = 1000;
	
	private static final String TAG = "BpDataLoader";
	private IBpOnDataLoaderCompleteListener mOnDataLoaderCompleteListener;
	private BpUrlLoader mUrlLoader;
	protected IBpDataLoaderParser mParser;
//	protected BpRunner mRunner;
	protected int mError = 0;
	
	public BpDataLoader(String url, String httpMethod, String contentIfPost, IBpDataLoaderParser parser) {
		this(url, httpMethod, contentIfPost, null, null,parser);
	}
	
	public BpDataLoader(String url, String httpMethod, String contentIfPost, String filePathIfPost, IBpDataLoaderParser parser) {
		this(url, httpMethod, contentIfPost, filePathIfPost, null, parser);
	}
	
	public BpDataLoader(String url, String httpMethod, String contentIfPost, 
			String filePathIfPost, HashMap<String, String> httpHeadersIfPost, IBpDataLoaderParser parser) {
		mParser = parser;
	//	mRunner = runner;
		mUrlLoader = new BpUrlLoader(url);
		mUrlLoader.setBpOnUrlLoaderCompleteListener(new IBpOnUrlLoaderCompleteListener() {
			
			public void onComplete(int respCode, Map<String, List<String>> headers,
					byte[] content, BpUrlLoader loader)	{
				Object obj = null;
				if(respCode == 200)	{
					String str = new String(content);
					Log.i(TAG, str);
					if(null != mParser) {
						obj = mParser.parse(content);
						mError = mParser.getError();
					}
				} else {
					mError = respCode;
				}
//				int status = mError == 0 ? RunnerInfo.STATUS_SUCCEED : RunnerInfo.STATUS_FAILED;
//				mRunner.setStatus(status);
				if(null != mOnDataLoaderCompleteListener){
					mOnDataLoaderCompleteListener.onComplete(mError, obj, headers, BpDataLoader.this);
				}
			}
		});
		
		mUrlLoader.setBpOnUrlLoaderErrorListener(new IBpOnUrlLoaderErrorListener() {
			
			public void onError(int errCode, BpUrlLoader loader) {
				mError = errCode;
		//		mRunner.setStatus(RunnerInfo.STATUS_FAILED);
				if(null != mOnDataLoaderCompleteListener) {
					mOnDataLoaderCompleteListener.onComplete(mError, null, null, BpDataLoader.this);
				}
			}
		});
		
		mUrlLoader.setHttpMethod(httpMethod, contentIfPost, filePathIfPost, httpHeadersIfPost);
	}
	
	@Override
	public void run() {
		Log.i(TAG, "run------------");
		if(null != mUrlLoader){
			mUrlLoader.run();
		}
	}

	@Override
	public void cancel() {
		if(null != mUrlLoader){
			mUrlLoader.cancel();
			super.cancel();
		}
	}
	
	public void setBpOnDataLoaderCompleteListener(IBpOnDataLoaderCompleteListener listener)	{
		if(null != listener){
			mOnDataLoaderCompleteListener = listener;
		}
	}
}
